from cProfile import label
from idlelib.colorizer import color_config

from pyecharts.charts import Bar, Timeline
from pyecharts.globals import *
from pyecharts.options import LabelOpts, TitleOpts

# def getFileDetail(addr):
#     """
#     获取文件详情信息
#     :param addr:
#     :return:
#     """
#     with open(addr, "r", encoding="UTF-8") as gdpLife:
#         gdpLife.readline()
#         gdpList = gdpLife.readlines()
#     return gdpList
# def getGdpDict(gdplist):
#     """
#     拼接年份和各个国家的GDP信息
#     :param gdplist:
#     :return:
#     """
#     yearSet = set()
#     yearList = {}
#     yearGdpList = []
#
#     ##获取所有年份 和各个国家GDP
#     for yearGdp in gdplist:
#         yearSet.add(yearGdp.split(",")[0])
#         yearGdpList.append(yearGdp.strip().split(","))
#     yearSetList = list(yearSet)
#     yearSetList.sort()
#     ## 拼接字典 key为年份
#     for setItem in yearSetList:
#         newYear = {setItem: []}
#         yearList.update(newYear)
#
#     ## 整合年份和当年的GDP
#     for yearItem in yearList:
#         for gdpitem in yearGdpList:
#             if gdpitem[0] == yearItem:
#                 yearList[yearItem].append([gdpitem[1], int(gdpitem[2])])
#
#     return yearList
# def getGJGdp(year, yearGdpAndGjList):
#     """
#     :param year:     年份
#     :param yearGdpAndGjList:  每年各个国家的GDP
#     :return:
#     """
#     gjs = []
#     gdps = []
#
#     yearGdpAndGjList[year].sort(key=lambda elm: elm[1])
#
#     for gjDe in  yearGdpAndGjList[year][len(yearGdpAndGjList[year])-3:len(yearGdpAndGjList[year])]:
#         gjs.append(gjDe[0])
#         gdps.append(gjDe[1])
#     return gjs, gdps
# def buildTimeline(yearGdpList):
#     """
#      构建时间线和条形图
#     :param yearGdpList:
#     :return:
#     """
#     ##构建时间线
#     line = Timeline({"theme": ThemeType.LIGHT})
#
#     for year in yearGdpList:
#         gjs, gdps = getGJGdp(year, yearGdpList)
#
#         ## 创建bar
#         bar = Bar()
#         bar.add_xaxis(gjs)
#         bar.add_yaxis(year, gdps, label_opts=LabelOpts(position="right"))
#         bar.reversal_axis()
#         line.add(bar, year)
#
#     ##配置时间线
#     line.add_schema(
#         play_interval=2000,  # 播放的时间间隔
#         is_timeline_show=True,  # 是否展示时间线
#         is_auto_play=True,  # 是否自动播放
#         is_loop_play=True  # 循环播放
#     )
#
#     line.render("gdp.html")
# gdpList = getFileDetail("D:\\GDP.txt")
# yearGdpList = getGdpDict(gdpList)
# buildTimeline(yearGdpList)

# ----------------------------------------------------视频教程
# 读取文件
f = open("D:\\GDP.txt", "r", encoding="UTF-8")
data_lines = f.readlines()
f.close()
# 删除第一个元素
data_lines.pop(0)

# 定义一个字典对象
data_dict = {}
for line in data_lines:
    year = line.split(",")[0]  # 年份
    country = line.split(",")[1]  # 国家
    gdp = float(line.split(",")[2])  # GDP

    ## 判断列表中是否有指定的key
    if year in data_dict:
        data_dict[year].append([country, gdp])
    else:
        data_dict[year] = []

##排序年份，自动按照key值排序 sorted(容器,reverse=True)
sorted(data_dict)

##注册时间线
line = Timeline({"theme":ThemeType.LIGHT})

##讲每年的gpr值按从大到小排列
for year in data_dict:
    data_dict[year].sort(key=lambda el: el[1], reverse=True)
    # 获取每年的前三个国家
    data_dict[year] = data_dict[year][0:3]

    x_data = list(map(lambda country: country[0], data_dict[year]))
    y_data = list(map(lambda gdp: gdp[1], data_dict[year]))

    # 反转数据
    x_data.reverse()
    y_data.reverse()

    bar = Bar()
    bar.add_xaxis(x_data)
    bar.add_yaxis("GDP(亿)", y_data, label_opts=LabelOpts(position="right"))
    bar.reversal_axis()
    bar.set_global_opts(
        title_opts=TitleOpts(title=f"{year}年前三GDP情况")
    )
    line.add(bar, year)

##配置时间线
line.add_schema(
    play_interval=2000,  # 播放的时间间隔
    is_timeline_show=True,  # 是否展示时间线
    is_auto_play=True,  # 是否自动播放
    is_loop_play=True  # 循环播放
)
line.render("1990-2020年全世界gdp前三国家.html")
